//
//  NotificationManager.m
//  HdStore
//
//  Created by zhenqiu on 15/11/23.
//  Copyright (c) 2015年 zhenqiu. All rights reserved.
//


#import <UIKit/UIKit.h>
#import "NotificationManager.h"
#import "CacheHelper.h"
#import "ClientApi.h"

@implementation NotificationManager{
  LoanWebTipManager *tipManager;
}
@synthesize manager;

+(id)getInstance
{
    static dispatch_once_t once ;
    static id instance = nil;
    dispatch_once(&once, ^{
        
        instance = [[NotificationManager alloc] init];
        
    });
    
    return instance;
    
}


- (id) init
{
    self =[super init];
    if( self)
    {
        
        // to do
      tipManager = [[LoanWebTipManager alloc] init];
    }
        
    return self;
    
    
}

-(void)startListen{
    
    if(![[CacheHelper getInstance] getCacheValue:PUSH_CODE_KEY]){
        
        [self registerRemoteNotification];
    }
    
    
}

-(void) registerRemoteNotification{
    
    if([[UIApplication sharedApplication] respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
    {
        
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil]];
        [[UIApplication sharedApplication] registerForRemoteNotifications];

    }
    
}


-(void) checkLaunchingOptions:(NSDictionary *)launchOptions{
    
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    
    NSDictionary* remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    
    if(remoteNotification)
    {
        
        GYNLog(@"remote start %@",remoteNotification);
       
    }
    
}


-(void)sendPushCode:(NSString *)pushCode{
    
}



#pragma mark -
#pragma mark Local Notification

-(void)registerLocalNotification{
    
    [self registerRemoteNotification];
    
}

-(void)sendLocalNotication:(id)info {
    
    
    UILocalNotification * localNotication = [[UILocalNotification alloc] init];
    NSDictionary *infodic = [[NSDictionary alloc] init];
    
    
    // to do init the notication
    localNotication.fireDate = nil;
    localNotication.timeZone = nil;
    localNotication.alertBody = nil;
    localNotication.repeatInterval = NSCalendarUnitDay;
    localNotication.userInfo = infodic;
    
    
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotication];
    
    
}

-(void)cancelLocalNoticaton:(NSString * ) key {
    NSArray * notifications = [UIApplication sharedApplication].scheduledLocalNotifications;
    for (UILocalNotification *notification  in notifications) {
        
        if([notification.userInfo valueForKey:key]){
            
            [[UIApplication sharedApplication] cancelLocalNotification:notification];
            break;
            
            
        }
        
    }
    
}

-(void)cancelAllNotications{
    
    [[UIApplication sharedApplication]cancelAllLocalNotifications];
}

- (void)getLoanBaoxianStatus{
  __weak typeof(self) weakSelf = self;
  [[ClientApi getInstance] getLoanBaoXianStatusWithCallBack:^(id  _Nullable responseObject, NSError * _Nullable error) {
    if (error) {
      GYNLog(@"error = %@",error);
    }else{
      GYNLog(@"res = %@",responseObject);
        weakSelf.loan_baoxian_status = [responseObject[@"status"] integerValue];
    }
  }];
}


- (void)getLoanListTipStatus{
  
  [[ClientApi getInstance] getZQLoanListStatuWithCallBack:^(id  _Nullable responseObject, NSError * _Nullable error) {
    if (error) {
      GYNLog(@"error = %@",error);
    }else{
      GYNLog(@"res = %@",responseObject);
      if (responseObject[@"items"]) {
        self.tipModel = [LoanWebTipModel getLoanWebTipModel:responseObject[@"items"]];
      }

    }
  }];
}





@end
